﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Game.Utilities;

using Game.Utilities.Log;
using Game.Utilities.Session;
using Web.Server.Controllers;
using Game.DataAccess.Factory;
using Game.DataAccess.DTO;
using GameLogHandler = Web.Server.Controllers.GameLogHandler;
using GameLogicHandler = Web.Server.Controllers.GameLogicHandler;

namespace Web.Server.API
{
    /// <summary>
    /// Summary description for apibetting
    /// </summary>
    public class apibetting : IHttpHandler, IRequiresSessionState
    {

        private int servicesid = int.Parse(ConfigurationManager.AppSettings["SSO_SERVICE_ID"]);
        private const int RecordPerpage = 6;
        public void ProcessRequest(HttpContext context)
        {
            switch (context.Request["p"])
            {
                case "setbet":
                    SetBetTest(context);
                    break;
                case "accHis":
                    GetAccountHistory(context);
                    break;
                case "jackHis":
                    GetJackpotHistory(context);
                    break;
                case "jackPot":
                    GetJackpot(context);
                    break;
            }
        }

        private void SetBet(HttpContext context)
        { 
            dynamic objReturn = new System.Dynamic.ExpandoObject();
            try
            {
                string betline = context.Request.QueryString["d"];
                betline = betline.Substring(0, betline.Length - 1);
                int roomId = int.Parse(context.Request.QueryString["roomId"]);
             
                if (AccountSession.AccountID > 0)
                {
                   
                    long betLogId=0;
                    var returnvalue = new GameLogicHandler().BetInput(0,roomId, betline, ref betLogId, servicesid);
                    if (returnvalue >= 0)
                    {
                        //step5
                        long responseOut=0;
                        var obj = new GameLogicHandler().BetOutput(betLogId,roomId,servicesid,ref responseOut);
                        if (obj != null)
                        {
                            objReturn.Data = obj;
                            objReturn.Response = 0;

                            if (obj.PrizeValue > 20000)
                            {
                                var obj1 = GameLogHandler.Instance.TryAddAward(AccountSession.AccountName, obj.PrizeValue);
                                ConnectionHandler.Instance.Clients.All.PublishAward(obj1);
                            }

                            ConnectionHandler.Instance.Clients.All.PublishJacpot(AbstractDaoFactory.Instance().CreateSlot().GetJackpotInfo());
                        }
                        else
                        {
                            objReturn.Response = "-999";
                        }

                    }
                    else
                    {
                        objReturn.Response = returnvalue;
                    }

                }
                else
                {
                    objReturn.Response = "-999";
                }

                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));


            }
            catch (Exception ex)
            {
                NLogManager.Warning("SetBet2:" + ex.ToString());
                objReturn.Response = "-1";
                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
            }

        }



        private void SetBetTest(HttpContext context)
        {
            dynamic objReturn = new System.Dynamic.ExpandoObject();
            try
            {
                string betline = context.Request.QueryString["d"];
                betline = betline.Substring(0, betline.Length - 1);
                int roomId = int.Parse(context.Request.QueryString["roomId"]);

                if (AccountSession.AccountID > 0)
                {

                    long betLogId = 0;
                    var returnvalue = new GameLogicHandler().BetInput(0, roomId, betline, ref betLogId, servicesid);
                    if (returnvalue >= 0)
                    {
                        //step5
                        long responseOut = 0;
                        var obj = new GameLogicHandler().BetOutputTest(betLogId, roomId, servicesid, ref responseOut);
                        if (obj != null)
                        {
                            objReturn.Data = obj;
                            objReturn.Response = 0;

                            if (obj.PrizeValue > 20000)
                            {
                                var obj1 = GameLogHandler.Instance.TryAddAward(AccountSession.AccountName, obj.PrizeValue);
                                ConnectionHandler.Instance.Clients.All.PublishAward(obj1);
                            }

                            ConnectionHandler.Instance.Clients.All.PublishJacpot(AbstractDaoFactory.Instance().CreateSlot().GetJackpotInfo());
                        }
                        else
                        {
                            objReturn.Response = "-999";
                        }

                    }
                    else
                    {
                        objReturn.Response = returnvalue;
                    }

                }
                else
                {
                    objReturn.Response = "-999";
                }

                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));


            }
            catch (Exception ex)
            {
                NLogManager.Warning("SetBet2:" + ex.ToString());
                objReturn.Response = "-1";
                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
            }


        }

        private void GetAccountHistory(HttpContext context)
        {
            dynamic objReturn = new System.Dynamic.ExpandoObject();
            int currPage = int.Parse(context.Request.QueryString["page"]);
            try
            {
                //List<AccountHistory> lst = AbstractDaoFactory.Instance().CreateRouttleDao().GetHistoryWinner(AccountSession.AccountID);
                if (AccountSession.AccountID > 0)
                {
                    var lst = AbstractDaoFactory.Instance().CreateSlot().GetHistoryWinner((int)AccountSession.AccountID);
                    var rows = new List<AccountHistory>();
                    if (lst.Count > 0)
                    {
                        rows = lst.Skip((currPage - 1) * RecordPerpage).Take(RecordPerpage).ToList();
                    }

                    objReturn.lst = rows;
                    objReturn.totalRecord = lst.Count;
                    context.Response.ContentType = "application/json";
                    context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
                }
                else
                {
                    objReturn.lst = new List<AccountHistory>();
                    objReturn.totalRecord = 0;
                    context.Response.ContentType = "application/json";
                    context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
                }
                
            }
            catch (Exception ex)
            {
                NLogManager.Warning("SET BET :" + ex.ToString());
                objReturn.lst = new List<AccountHistory>();
                objReturn.totalRecord = 0;
                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
            }
        }

        private void GetJackpotHistory(HttpContext context)
        {
            dynamic objReturn = new System.Dynamic.ExpandoObject();
            int currPage = int.Parse(context.Request.QueryString["page"]);
            try
            {
                var lst = AbstractDaoFactory.Instance().CreateSlot().GetHistoryJackport();
                var rows = new List<HistoryJackport>();

                if (lst.Count > 0)
                {
                    rows = lst.Skip((currPage - 1) * RecordPerpage).Take(RecordPerpage).ToList();
                }

                objReturn.lst = rows;
                objReturn.totalRecord = lst.Count;
                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
            }
            catch (Exception ex)
            {
                NLogManager.Warning("SET BET :" + ex.ToString());
                objReturn.lst = new List<HistoryJackport>();
                objReturn.totalRecord = 0;
                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
            }
        }

        private void GetJackpot(HttpContext context)
        {
            dynamic objReturn = new System.Dynamic.ExpandoObject();
             
            try
            {
                var lst = AbstractDaoFactory.Instance().CreateSlot().GetJackpotInfo();

                objReturn.lst = lst;
                objReturn.response = 0;
                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
            }
            catch (Exception ex)
            {
                NLogManager.Warning("SET BET :" + ex.ToString());
                objReturn.response = -1;
                context.Response.ContentType = "application/json";
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(objReturn));
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}